'use strict';

const Controller = require('egg').Controller;

class BlogController extends Controller {
  /**
   *
   * @api {POST} /api/blog/create 客户端-创建博客(需要token)
   * @apiName 创建博客
   * @apiGroup 博客模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {String} title   博客标题
   * @apiParam  {String} content 博客内容
   * @apiParam  {String} cover   博客背景图
   * @apiParam  {Array} classifyArr 博客所属分类
   * @apiParam  {Number} projectId 项目id
   *
   *
   * @apiParamExample  {type} Request-Example:
   * {
          "title":"哈哈哈",
          "content":"哈哈哈",
          "cover":"https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",
          "classifyArr":[1,3,4],
          "projectId":4
   * }
   *
   *
   * @apiSuccessExample {type} Success-Response:
   * {
          "code": 200,
          "msg": "成功操作",
          "data": {
              "read_count": 0,        //博客浏览次数
              "like_count": 0,        //博客点赞次数
              "comment_count": 0,     //博客评论次数
              "id": 58,               //博客id
              "userId": 20,           //博客作者id
              "userName": "qwer",     //博客作者昵称
              "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //博客作者头像
              "title": "哈哈",         //博客标题
              "content": "哈哈",       //博客内容
              "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",    //博客封面图
              "projectId":4,           //项目id
              "updatedAt": "2021-10-09T02:54:20.570Z",                 //更新时间
              "createdAt": "2021-10-09T02:54:20.570Z"                  //创建时间
          }
      }
   *
   *
   */

  // 创建博客(token)
  async create() {
    const { app, ctx, service } = this;
    const checkRes = new ctx.helper.checkData(
      this,
      'title',
      'content',
      'cover',
      'classifyArr'
    ).checkDataRes;

    try {
      if (checkRes) {
        const res = await service.blog.blog.create();
        if (res == '项目id错误') {
          return ctx.helper.errorModule(res);
        }
        if (res == '用户不存在') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('操作失败');
    }
  }
  /**
   *
   * @api {GET} /api/blog/getAllBlog?projectId=4&limit=10&offset=0 管理台-获取所有博客(分页)
   * @apiName 获取所有博客
   * @apiGroup 博客模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {Number} projectId  项目id
   * @apiParam  {Number} limit  返回博客数
   * @apiParam  {Number} offset 博客偏移数
   *
   *
   *
   * @apiSuccessExample {type} Success-Response:
   * {
      "code": 200,
      "msg": "成功操作",
      "data": {
          "count": 1,                 //博客总数 用于分几页
          "rows": [
              {
                  "id": 58,                       //博客id
                  "userId": 20,                   //博客作者id
                  "userName": "qwer",             //博客作者昵称
                  "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //博客作者头像
                  "title": "10-09",               //博客标题
                  "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",        //博客封面图
                  "content": "10-09",             //博客内容
                  "read_count": 0,                //博客浏览次数
                  "like_count": 0,                //博客点赞次数
                  "comment_count": 0,             //博客评论次数
                  "createdAt": "2021-10-09T02:54:20.000Z",        //创建时间
                  "updatedAt": "2021-10-09T02:54:20.000Z",        //更新时间
                  "deletedAt": null,
                  "BlogClassifies": [                 //博客所属分类
                      {
                          "classifyId": 1,
                          "classifyName": "HTML"
                      },
                      {
                          "classifyId": 3,
                          "classifyName": "JS"
                      },
                      {
                          "classifyId": 4,
                          "classifyName": "egg.js"
                      }
                  ],
                  "LikeBlogs": [         //点赞该博客的用户id集合
                  {
                      "likeUserId": 16
                  }
              ]
              }
          ]
      }
  }
   *
   *
   */
  // 获取所有博客
  async getAllBlog() {
    const { app, ctx, service } = this;
    const checkRes = new ctx.helper.checkData(this, 'projectId').checkDataRes;

    if (checkRes) {
      const res = await service.blog.blog.getAllBlog();
      if (res == '项目id错误') {
        return ctx.helper.errorModule(res);
      }
      return ctx.helper.successModule(res);
    }
  }

  /**
   *
   * @api {GET} /api/blog/search?id=58&projectId=4 客户端-根据id查找博客(如果有token值会有历史记录)
   * @apiName 根据id查找博客
   * @apiGroup 博客模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {Number} id 博客id
   * @apiParam  {Number} projectId 项目id
   *
   *
   *
   * @apiSuccessExample {type} Success-Response:
     {
          "code": 200,
          "msg": "成功操作",
          "data": {
              "id": 58,                   //博客id
              "userId": 29,               //博客作者id
              "userName": "qwer",         //博客作者昵称
              "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //博客作者头像
              "title": "10-09",           //博客作者昵称
              "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",    //博客封面图
              "content": "10-09",         //博客内容
              "read_count": 3,            //博客浏览次数
              "like_count": 1,            //博客点赞次数
              "comment_count": 0,         //博客评论次数
              "projectId":4,              //项目id
              "createdAt": "2021-10-09T02:54:20.000Z",    //创建时间
              "updatedAt": "2021-10-09T03:40:06.000Z",    //更新时间
              "deletedAt": null,
              "BlogClassifies": [             //所属分类
                  {
                      "classifyId": 1,
                      "classifyName": "HTML"
                  },
                  {
                      "classifyId": 3,
                      "classifyName": "JS"
                  },
                  {
                      "classifyId": 4,
                      "classifyName": "egg.js"
                  }
              ],
              "LikeBlogs": [                  //点赞的用户的id集合
                  {
                      "likeUserId": 16
                  }
              ],
              "follow": {
              "count": 2,         //作者粉丝数
              "rows": [
                  {
                      "id": 31,               //关注记录id
                      "followUserId": 28,     //关注作者的用户id
                      "beFollowUserId": 29,   //被关注作者的id
                      "projectId": 4,         //项目id
                      "createdAt": "2021-10-12T08:33:10.000Z",    //关注时间
                      "updatedAt": "2021-10-12T08:33:10.000Z",
                      "deletedAt": null
                  },
                  {
                      "id": 32,
                      "followUserId": 40,
                      "beFollowUserId": 29,
                      "projectId": 4,
                      "createdAt": "2021-10-20T07:28:20.000Z",
                      "updatedAt": "2021-10-20T07:28:20.000Z",
                      "deletedAt": null
                  }
              ]
              }
          }
      }
   *
   *
   */
  // 通过id查找博客
  async searchById() {
    const { ctx, service } = this;
    const checkRes = new this.ctx.helper.checkData(this, 'id').checkDataRes;

    try {
      if (checkRes) {
        const res = await service.blog.blog.searchById();
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('博客id不存在');
    }
  }

  /**
   *
   * @api {GET} /api/blog/searchByClassifyId?id=3&projectId=4&limit=5&offset=0 客户端-根据分类id查找博客
   * @apiName 根据分类id查找博客
   * @apiGroup 博客模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {Number} id 分类id
   * @apiParam  {Number} projectId 项目id
   * @apiParam  {String} limit 博客返回量
   * @apiParam  {String} offset 博客偏移量
   *
   *
   *
   * @apiSuccessExample {type} Success-Response:
   {
      "code": 200,
      "msg": "成功操作",
      "data": {
          "count": 1,
          "rows": [
              {
                  "blogId": 58,               //博客id
                  "classifyId": 1,            //分类id
                  "classifyName": "HTML",     //分类名
                  "projectId":4,              //项目id
                  "Blog": {
                      "userId": 20,           //作者id
                      "userName": "qwer",     //作者昵称
                      "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //作者头像
                      "title": "10-09",       //博客标题
                      "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",        //博客封面图
                      "content": "10-09",     //博客内容
                      "read_count": 4,        //博客浏览次数
                      "like_count": 1,        //博客点赞次数
                      "comment_count": 0      //博客评论次数
                  },
                  "LikeBlog": {               //点赞的用户的id集合
                      "count": 1,             //点赞总人数
                      "rows": [
                          {
                              "likeUserId": 16
                          }
                      ]
                  }
              }
          ]
      }
  }
   *
   *
   */
  /**
  *
  * @api {GET} /api/blog/searchByClassifyId?id=3&projectId=4&limit=5&offset=0 客户端-根据分类id查找博客
  * @apiName 根据分类id查找博客
  * @apiGroup 博客模块
  * @apiVersion  1.0.0
  *
  *
  * @apiParam  {Number} id 分类id
  * @apiParam  {Number} projectId 项目id
  * @apiParam  {String} limit 博客返回量
  * @apiParam  {String} offset 博客偏移量
  *
  *
  *
  * @apiSuccessExample {type} Success-Response:
  {
     "code": 200,
     "msg": "成功操作",
     "data": {
         "count": 1,
         "rows": [
             {
                 "blogId": 58,               //博客id
                 "classifyId": 1,            //分类id
                 "classifyName": "HTML",     //分类名
                 "projectId":4,              //项目id
                 "Blog": {
                     "userId": 20,           //作者id
                     "userName": "qwer",     //作者昵称
                     "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //作者头像
                     "title": "10-09",       //博客标题
                     "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",        //博客封面图
                     "content": "10-09",     //博客内容
                     "read_count": 4,        //博客浏览次数
                     "like_count": 1,        //博客点赞次数
                     "comment_count": 0      //博客评论次数
                 },
                 "LikeBlog": {               //点赞的用户的id集合
                     "count": 1,             //点赞总人数
                     "rows": [
                         {
                             "likeUserId": 16
                         }
                     ]
                 }
             }
         ]
     }
 }
  *
  *
  */
  // 根据分类id查找博客
  async searchByClassifyId() {
    const { app, ctx, service } = this;
    const checkRes = new this.ctx.helper.checkData(this, 'id', 'projectId')
      .checkDataRes;

    try {
      if (checkRes) {
        const res = await service.blog.blog.searchByClassifyId();
        if (res == '项目id错误') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('分类id不存在');
    }
  }

  /**
   *
   * @api {GET} /api/blog/searchByInput?keyword=10&projectId=4 客户端-根据关键词搜索博客
   * @apiName 根据关键词搜索博客
   * @apiGroup 博客模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {String} keyword 关键词
   * @apiParam  {Number} projectId 项目id
   *
   *
   * @apiSuccessExample {type} Success-Response:
      {
      "code": 200,
      "msg": "成功操作",
      "data": [
          {
              "id": 58,               //博客id
              "userId": 20,           //博客作者id
              "userName": "qwer",     //博客作者昵称
              "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //博客作者头像
              "title": "10-09",       //博客标题
              "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",    //博客封面图
              "content": "10-09",     //博客内容
              "read_count": 5,        //博客浏览次数
              "like_count": 1,        //博客点赞次数
              "comment_count": 0,     //博客评论次数
              "projectId":4,           //项目id
              "createdAt": "2021-10-09T02:54:20.000Z",    //博客创建时间
              "updatedAt": "2021-10-09T06:28:05.000Z",    //博客更新时间
              "deletedAt": null
          }
      ]
  }
   *
   *
   */
  // 根据关键词查找博客
  async searchByInput() {
    const { app, ctx, service } = this;
    const checkRes = new this.ctx.helper.checkData(this, 'keyword', 'projectId')
      .checkDataRes;
    try {
      if (checkRes) {
        const res = await service.blog.blog.searchByInput();
        if (res == '项目id错误') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
    }
  }

  /**
   *
   * @api {GET} /api/blog/getMyBlog?projectId=4&limit=10&offset=0 客户端-获取用户自己的博客(需要token)
   * @apiName 获取用户自己的博客
   * @apiGroup 博客模块
   * @apiVersion  1.0.0
   *
   * @apiParam  {Number} projectId 项目id
   * @apiParam  {String} limit 博客返回量
   * @apiParam  {String} offset 博客偏移量
   *
   * @apiSuccessExample {type} Success-Response:
      {
      "code": 200,
      "msg": "成功操作",
      "data": {
          "count": 2,                 //博客总数
          "rows": [
              {
                  "id": 58,               //博客id
                  "userId": 20,           //博客作者id
                  "userName": "qwer",     //博客作者昵称
                  "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //博客作者头像
                  "title": "10-09",       //博客标题
                  "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",        //博客封面图
                  "content": "10-09",     //博客内容
                  "read_count": 5,        //博客浏览次数
                  "like_count": 1,        //博客点赞次数
                  "comment_count": 0,     //博客评论次数
                  "projectId":4,           //项目id
                  "createdAt": "2021-10-09T02:54:20.000Z",    //博客创建时间
                  "updatedAt": "2021-10-09T06:28:05.000Z",    //博客更新时间
                  "deletedAt": null,
                  "BlogClassifies": [         //博客所属分类
                      {
                          "classifyId": 1,
                          "classifyName": "HTML"
                      },
                      {
                          "classifyId": 3,
                          "classifyName": "JS"
                      },
                      {
                          "classifyId": 4,
                          "classifyName": "egg.js"
                      }
                  ]
              },
              {
                  "id": 59,
                  "userId": 20,
                  "userName": "qwer",
                  "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",
                  "title": "14-30",
                  "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",
                  "content": "14-30",
                  "read_count": 1,
                  "like_count": 0,
                  "comment_count": 0,
                  "createdAt": "2021-10-09T06:27:57.000Z",
                  "updatedAt": "2021-10-09T06:28:09.000Z",
                  "deletedAt": null,
                  "BlogClassifies": [
                      {
                          "classifyId": 1,
                          "classifyName": "HTML"
                      },
                      {
                          "classifyId": 2,
                          "classifyName": "CSS"
                      },
                      {
                          "classifyId": 3,
                          "classifyName": "JS"
                      },
                      {
                          "classifyId": 4,
                          "classifyName": "egg.js"
                      }
                  ]
              }
          ]
      }
  }
   *
   *
   */
  // 获取用户自己的博客(token)
  async getMyBlog() {
    const { app, ctx, service } = this;
    const checkRes = new this.ctx.helper.checkData(this, 'projectId')
      .checkDataRes;

    try {
      // 如果有缺少参数
      if (checkRes) {
        const res = await service.blog.blog.getMyBlog();
        if (res == '项目id错误') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule(error);
    }
  }
  /**
   *
   * @api {PUT} /api/blog/updataBlog 客户端-修改博客(需要token)
   * @apiName 修改博客
   * @apiGroup 博客模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {Number} id 博客id
   * @apiParam  {Number} projectId 项目id
   * @apiParam  {String} title 博客标题
   * @apiParam  {String} content 博客内容
   * @apiParam  {String} cover 博客封面图
   * @apiParam  {Array} classifyArr 博客分类数组
   *
   *
   *
   *
   * @apiSuccessExample {type} Success-Response:
      {
          "code": 200,
          "msg": "修改成功",
          "data": []
      }
   *
   *
   */
  // 更新修改博客
  async updateBlog() {
    const { app, ctx, service } = this;
    const checkRes = new this.ctx.helper.checkData(
      this,
      'id',
      'title',
      'content',
      'cover',
      'classifyArr',
      'projectId'
    ).checkDataRes;

    try {
      if (checkRes) {
        const res = await service.blog.blog.updateBlog();
        if (res == '项目id错误') {
          return ctx.helper.errorModule(res);
        }
        if (res == '您不是文章作者') {
          return ctx.helper.errorModule(res);
        }
        if (res == '博客id不存在') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('博客id不存在');
    }
  }
  /**
   *
   * @api {DELETE} /api/blog/deleteBlog 客户端-删除博客(需要token)
   * @apiName 删除博客
   * @apiGroup 博客模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {Number} id 博客id
   * @apiParam  {Number} projectId 项目id
   *
   * @apiSuccessExample {type} Success-Response:
      {
          "code": 200,
          "msg": "删除成功",
          "data": []
      }
   *
   *
   */
  // 删除博客
  async deleteBlog() {
    const { app, ctx, service } = this;
    const checkRes = new this.ctx.helper.checkData(this, 'id', 'projectId')
      .checkDataRes;

    try {
      if (checkRes) {
        const res = await service.blog.blog.deleteBlog();
        if (res == '项目id错误') {
          return ctx.helper.errorModule(res);
        }
        if (res == '您不是文章作者') {
          return ctx.helper.errorModule(res);
        }
        if (res == '博客id不存在') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('博客id不存在');
    }
  }
  /**
   *
   * @api {POST} /api/blog/ldecodeikeBlog 客户端-点赞/取消点赞博客(需要token)
   * @apiName 点赞博客
   * @apiGroup 博客模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {Number} id 博客id
   * @apiParam  {Number} projectId 项目id
   *
   *
   * @apiSuccessExample {type} Success-Response:
      {
          "code": 200,
          "msg": "点赞成功"/"取消点赞成功",
          "data": []
      }
   *
   *
   */
  // 点赞博客
  async likeBlog() {
    const { app, ctx, service } = this;
    const checkRes = new this.ctx.helper.checkData(this, 'id', 'projectId')
      .checkDataRes;

    try {
      if (checkRes) {
        const res = await service.blog.blog.likeBlog();
        if (res == '项目id错误') {
          return ctx.helper.errorModule(res);
        }
        if (res == '博客id不存在') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('博客id不存在');
    }
  }
  /**
   *
   * @api {GET} /api/blog/getHistory?projectId=11&limit=10&offset=0 客户端-获取浏览博客历史(按时间大到小顺序)(需要token)
   * @apiName 获取浏览博客历史
   * @apiGroup 博客模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {Number} projectId 项目id
   * @apiParam  {Number} limit 历史记录返回条数
   * @apiParam  {Number} offset 历史记录偏移数
   *
   *
   * @apiSuccessExample {type} Success-Response:
      {
          "code": 200,
          "msg": "成功操作",
          "data": {
              "count": 2,                 //历史记录总数
              "rows": [
                  {
                      "id": 38,                   //历史记录id
                      "userId": 40,               //浏览博客的用户的id
                      "blogId": 178,              //博客id
                      "projectId": 11,            //项目id
                      "createdAt": "2021-10-18T02:54:12.000Z",    //第一次浏览时间
                      "updatedAt": "2021-10-18T02:54:12.000Z",    //最近一次浏览时间
                      "deletedAt": null,
                      "Blog": {
                          "userId": 39,                   //博客作者is
                          "userName": "hahaha",           //博客作者昵称
                          "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png", //博客作者头像
                          "projectId": 11,                //项目id
                          "title": "blog2",               //博客标题
                          "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",    //博客封面图
                          "content": "blog2",             //博客内容
                          "read_count": 1,                //博客浏览次数
                          "like_count": 1,                //博客点赞次数
                          "comment_count": 0              //博客评论次数
                      },
                      "LikeBlog": {
                          "count": 1,                     //博客点赞数量
                          "rows": [                       //博客点赞用户合集
                              {
                                  "likeUserId": 40        //博客点赞用户id
                              }
                          ]
                      }
                  },
                  {
                      "id": 37,
                      "userId": 40,
                      "blogId": 175,
                      "projectId": 11,
                      "createdAt": "2021-10-18T02:34:45.000Z",
                      "updatedAt": "2021-10-18T02:35:09.000Z",
                      "deletedAt": null,
                      "Blog": {
                          "userId": 40,
                          "userName": "blog",
                          "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",
                          "projectId": 11,
                          "title": "blog",
                          "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",
                          "content": "blog",
                          "read_count": 5,
                          "like_count": 1,
                          "comment_count": 0
                      },
                      "LikeBlog": {
                          "count": 1,
                          "rows": [
                              {
                                  "likeUserId": 40
                              }
                          ]
                      }
                  }
              ]
          }
      }
   *
   *
   */
  // 获取浏览博客历史
  async getHistory() {
    const { app, ctx, service } = this;
    const checkRes = new this.ctx.helper.checkData(this, 'projectId')
      .checkDataRes;

    if (checkRes) {
      const res = await service.blog.blog.getHistory();
      if (res == '项目id错误') {
        return ctx.helper.errorModule(res);
      }
      return ctx.helper.successModule(res);
    }
  }
}
module.exports = BlogController;
